package com.example.back.controller;

import com.example.back.model.Room;
import com.example.back.model.User;
import com.example.back.service.RoomService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 房间控制层
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    private static final Logger logger = LogManager.getLogger(RoomController.class);

    @Resource
    private RoomService roomService;

    @RequestMapping("/getRoomList")
    @ResponseBody
    public List<Room> getRoomList(){
        List<Room> roomList = roomService.findAllRoom();
        return roomList;
    }

    @RequestMapping("/getRoomUserList")
    @ResponseBody
    public List<User> getRoomUserList(@RequestParam("name") String name){
        List<User> userList = roomService.findRoomUser(name);
        return userList;
    }

    @RequestMapping("/insertRoom")
    public String insertRoom(String name, double cost, String type, int maxCount) {
        String rs = "";
        if (roomService.insertRoom(name, cost, type, maxCount)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>添加房间成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>添加房间失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updateRoom")
    public String updateRoom(int id, double cost) {
        String rs = "";
        if (roomService.updateRoom(id, cost)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>房间费用更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>房间费用更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/deleteRoom")
    public String deleteRoom(int id) {
        String rs = "";
        if (roomService.deleteRoom(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除房间成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除房间失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/createRoom")
    public boolean createRoom(@RequestParam("account") String account, @RequestParam("name") String name, @RequestParam("state") String state, @RequestParam("flag") String flag){
        int state_int = Integer.parseInt(state);
        logger.info("account: " + account + ";name: " + name + ";state:" + state_int + ",remark:" + flag);
        if (roomService.createRoom(account, name, state_int, flag).equals("success")){
            return true;
        }
        return false;
    }

    @RequestMapping("/joinRoom")
    public boolean createRoom(@RequestParam("account") String account, @RequestParam("name") String name){
        logger.info("account: " + account + ";name: " + name);
        if (roomService.joinRoom(account, name).equals("success")){
            return true;
        }
        return false;
    }

    @RequestMapping("/leaveRoom")
    public boolean leaveRoom(@RequestParam("account") String account, @RequestParam("name") String name){
        logger.info("account: " + account + ";name: " + name);
        if (roomService.leaveRoom(account, name).equals("success")){
            return true;
        }
        return false;
    }

    @RequestMapping("/atRoom")
    @ResponseBody
    public Room atRoom(@RequestParam("account") String account){
        Room room = roomService.findAtRoom(account);
        return room;
    }
}
